package num783;

import java.util.LinkedList;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/9 0:10
 * @Desc : 中序遍历时，将值放进列表中，并且顺便做一个比较。
 */
public class Solution {
  int min = Integer.MAX_VALUE;
  private LinkedList<Integer> stack = new LinkedList<>();

  public int minDiffInBST(TreeNode root) {
    recursion(root);
    return min;
  }

  public void recursion(TreeNode root) {
    if (root == null) return;
    recursion(root.left);
    Integer pre = stack.peek();
    if (pre != null) {
      min = Math.min(Math.abs(pre - root.val), min);
    }
    stack.push(root.val);
    recursion(root.right);
  }

  public static void main(String[] args) {
    TreeNode root = new TreeNode(4);
    root.left  = new TreeNode(2);
    root.left.left  = new TreeNode(1);
    root.left.right  = new TreeNode(3);
    root.right  = new TreeNode(6);
    int min = new Solution().minDiffInBST(root);
    System.out.println(min);
  }
}
